package accueil.pack;

import chemin.pack.R;
import chemin.pack.R.layout;
import chemins.pack.*;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.view.*;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import communication.AndroidConnection;
import communication.IncomingHandler;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class SousMenu extends Activity {

        AndroidConnection conn;
    
    
        ArrayList<Parcours> tableauDeParcours = new ArrayList();
        Parcours parcours = new Parcours();
        int numeroParcours,i;
        boolean running = true;
        Thread sendData;
        
        int credit;
        int creditInitial;
        LinearLayout.LayoutParams jaugeCreditD= new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,0);
        LinearLayout.LayoutParams jaugeCreditG= new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,0);
        View view;
        
        int distanceParcourueTotale;
        String assistanceType;

        @Override
        protected void onStop() {
            running = false;
            if (sendData != null) sendData.interrupt();
            conn.close();
            super.onStop();
            }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                exitActivity(view);
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
         
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        numeroParcours = 0;
            
            IncomingHandler handler = new IncomingHandler(){

            
            public void handleDistChange(String msg) {
                
            }

            @Override
            public void handleReconnect() {
                
            }

            @Override
            public void handleSendMsg(final String string) {
                
                sendData = new Thread(){

                    @Override
                    public void run() {
                        
                        while(running){
                            Log.i("IncomingHandler", "trying to send data...");
                            if(conn.write(string)){
                                Log.i("IncomingHandler", "sended message to request data from computer");
                                return;
                            }
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
                                return;
                            }
                        }
                    }
                };
                sendData.start();
            }

            @Override
            public void handleReset() {
                
            }

            @Override
            public void handleData(String data) {
                if(!data.startsWith("slopeData:")){
                    Log.i("IncomingHandler", "wrong data type was sent");
                    return;
                }

                
                data = data.substring(10);
                int index1 = data.indexOf("\t");
                
                String dataPart1,dataPart2;
                
                
                
                
                if(data.equals("finTransmission"))
                {
                    conn.close();
                    if(numeroParcours != 0)
                    {
                        dessinerApercu(numeroParcours);
                        tableauDeParcours.add(numeroParcours-1, parcours);
                    }
                    else
                    {
                        findViewById(R.id.sousMenuErreur).setVisibility(View.VISIBLE);
                    }
                    findViewById(R.id.sousMenuBasContenant).setVisibility(View.VISIBLE);
                    findViewById(R.id.progressBarSousMenu).setVisibility(View.GONE);
           
                }
                else
                {
                    dataPart1 =data.substring(0, index1);                    
                    dataPart2 =data.substring(index1+ 1);

                
                   if(dataPart1.equals("titre1:"))
                    {
                        
                        
                        if(numeroParcours > 0)
                        {
                           
                           tableauDeParcours.add(numeroParcours-1, parcours);                          
                           dessinerApercu(numeroParcours);
                           
                           
                        }
                        parcours = new Parcours();
                        numeroParcours++;
                        i=0;
                        
                        
                        
                        parcours.setTitre1(dataPart2);
                        
                        
                        if(numeroParcours == 1)
                        {
                          ((TextView) findViewById(R.id.sousMenuChoix1Text1)).setText(dataPart2);
                          findViewById(R.id.sousMenuChoix1).setVisibility(View.VISIBLE);
                          
                        }
                        if(numeroParcours == 2)
                        {
                          ((TextView) findViewById(R.id.sousMenuChoix2Text1)).setText(dataPart2);
                          findViewById(R.id.sousMenuChoix2).setVisibility(View.VISIBLE);
                        }
                        if(numeroParcours == 3)
                        {
                          ((TextView) findViewById(R.id.sousMenuChoix3Text1)).setText(dataPart2);
                          findViewById(R.id.sousMenuChoix3).setVisibility(View.VISIBLE);
                        }
                        if(numeroParcours == 4)
                        {
                          ((TextView) findViewById(R.id.sousMenuChoix4Text1)).setText(dataPart2);
                          findViewById(R.id.sousMenuChoix4).setVisibility(View.VISIBLE);
                        }
                        
                        
                        //Log.i("TableauParcours","TableauParcours["+ numeroParcours+"]");
                    }
                    else
                    {
                        if(dataPart1.equals("titre2:"))
                        {
                            parcours.setTitre2(dataPart2);
                            
                            if((numeroParcours == 1)&&(!dataPart2.isEmpty()))
                            {
                                ((TextView) findViewById(R.id.sousMenuChoix1Text2)).setText(dataPart2);  
                            }
                            if((numeroParcours == 2)&&(!dataPart2.isEmpty()))
                            {
                                ((TextView) findViewById(R.id.sousMenuChoix2Text2)).setText(dataPart2);  
                            }
                            if((numeroParcours == 3)&&(!dataPart2.isEmpty()))
                            {
                                ((TextView) findViewById(R.id.sousMenuChoix3Text2)).setText(dataPart2);  
                            }
                            if((numeroParcours == 4)&&(!dataPart2.isEmpty()))
                            {
                                ((TextView) findViewById(R.id.sousMenuChoix4Text2)).setText(dataPart2);  
                            }
                        }
                        else
                        {
                            if(dataPart1.equals("titre3:"))
                            {
                                parcours.setTitre3(dataPart2);
                                
                                if((numeroParcours == 1)&&(!dataPart2.isEmpty()))
                                {
                                    ((TextView) findViewById(R.id.sousMenuChoix1Text3)).setText(dataPart2);  
                                }
                                if((numeroParcours == 2)&&(!dataPart2.isEmpty()))
                                {
                                    ((TextView) findViewById(R.id.sousMenuChoix2Text3)).setText(dataPart2);  
                                }
                                if((numeroParcours == 3)&&(!dataPart2.isEmpty()))
                                {
                                    ((TextView) findViewById(R.id.sousMenuChoix3Text3)).setText(dataPart2);  
                                }
                                if((numeroParcours == 4)&&(!dataPart2.isEmpty()))
                                {
                                    ((TextView) findViewById(R.id.sousMenuChoix4Text3)).setText(dataPart2);  
                                }
                            
                            }
                            else
                            {
                                if(dataPart1.equals("distance:"))
                                {
                                    
                                }
                                else
                                {
                                    
                                    parcours.getTab()[i] = new Point();
                                    
                                    parcours.getTab()[i].setDistance(Integer.parseInt(dataPart1));
                                    parcours.getTab()[i].setHauteur(Integer.parseInt(dataPart2));
                                    
                                    //Log.i("TableauParcours "+numeroParcours,"point["+ i+"] + distance = " + tableauDeParcours.get(numeroParcours-1).getTab()[i].getdistance());
                                    //Log.i("TableauParcours "+numeroParcours,"point["+ i+"] + hauteur = " + tableauDeParcours.get(numeroParcours-1).getTab()[i].gethauteur());
                                    i++;
                                    parcours.setSize(i);
                                    
                                    
                                }

                            }
                        }

                        
                    } 
                }
                
                
                
                
            }

            
            public void handleMoveChange(String msg) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
                    
     };         
                conn = new AndroidConnection(handler);
                conn.initialize();
                handler.sendMessage(handler.obtainMessage(IncomingHandler.SEND_MESSAGE,"requestData"));
	        /* Set full screen view*/
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
	                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);

                                
                Bundle extras=getIntent().getExtras();
                credit =extras.getInt("credit");
                this.credit = credit;
                creditInitial =extras.getInt("creditInitial");
                this.creditInitial = creditInitial;
                
                distanceParcourueTotale = extras.getInt("distanceParcourueTotale");
                this.distanceParcourueTotale = distanceParcourueTotale;
                
                assistanceType =extras.getString("assistanceType");
                this.assistanceType = assistanceType;
                
	        setContentView(R.layout.sous_menu_interface); 
                
                if(creditInitial == -1)
                {
                    findViewById(R.id.sousMenuJaugeBase).setVisibility(View.GONE);                            
                }

                
                initialisationText();
                evolutionCredit();
                
                findViewById(R.id.sousMenuBasContenant).setVisibility(View.GONE);
                findViewById(R.id.progressBarSousMenu).setVisibility(View.VISIBLE);
                findViewById(R.id.sousMenuChoix1).setVisibility(View.GONE);
                findViewById(R.id.sousMenuChoix2).setVisibility(View.GONE);
                findViewById(R.id.sousMenuChoix3).setVisibility(View.GONE);
                findViewById(R.id.sousMenuChoix4).setVisibility(View.GONE);
                }
                
                
	  

	   public void versChemin1(View v)
	    {
                
               
	         Intent intent2 = new Intent(SousMenu.this, CheminActivity.class);                 
                 intent2 = creationBundle(1,intent2);
                 
                 finish();
	         startActivity(intent2);
  
	    }
           public void versChemin2(View v)
	    {
                
                 
	         Intent intent2 = new Intent(SousMenu.this, CheminActivity.class);
                 intent2 = creationBundle(2,intent2);
                 
                 finish();
	         startActivity(intent2);
  
	    }
           public void versChemin3(View v)
	    {
                
                 
	         Intent intent2 = new Intent(SousMenu.this, CheminActivity.class);
                 intent2 = creationBundle(3,intent2);
                 
                 finish();
	         startActivity(intent2);
  
	    }
           public void versChemin4(View v)
	    {
            
                 
	         Intent intent2 = new Intent(SousMenu.this, CheminActivity.class);
                 intent2 = creationBundle(4,intent2);
                 
                 finish();
	         startActivity(intent2);
  
	    }

	   
	    public void exitActivity(View v)
	    {
                
                 Intent intent2 = new Intent(SousMenu.this, Menu.class);
                 intent2.putExtra("typeDeChemin","monChemin");
                 intent2.putExtra("creditInitial",creditInitial);
                 intent2.putExtra("credit",credit);
                 intent2.putExtra("distanceParcourueTotale",distanceParcourueTotale);
                 intent2.putExtra("assistanceType",assistanceType);
                 
                 finish();
	         startActivity(intent2);

	    }
            
            public Intent creationBundle(int numeroParcours, Intent intent2)
            {
                int i = 0;
                intent2.putExtra("creditInitial",creditInitial);
                intent2.putExtra("credit",credit);
                intent2.putExtra("distanceParcourueTotale",distanceParcourueTotale);
                intent2.putExtra("typeDeChemin","cheminTDF");
                intent2.putExtra("assistanceType",assistanceType);
                
                while(i< tableauDeParcours.get(numeroParcours-1).getSize() )
                {

                    intent2.putExtra("indice"+i+"distance",tableauDeParcours.get(numeroParcours-1).getTab()[i].getDistance());
                    intent2.putExtra("indice"+i+"hauteur",tableauDeParcours.get(numeroParcours-1).getTab()[i].getHauteur());
                    i++;
                }
                intent2.putExtra("size",tableauDeParcours.get(numeroParcours-1).getSize());
                
                
                
                
                
                
                return intent2;
            }
            
            public Void dessinerApercu(int numeroParcours)
            {
                int x_max=0,x_min=0,y_max=0,y_min=0;
                

                
                ViewGroup layout = (ViewGroup) findViewById(R.id.sousMenuApercu);
               

                int largeurTablette = 1280,hauteurTablette = 800;
                
                if(numeroParcours == 1)
                {
                     x_max = 600;
                     x_min = 40;
                     y_max = 400;
                     y_min = 280; 
                }
                
                if(numeroParcours == 2)
                {
                     x_max = 600;
                     x_min = 40;
                     y_max = 400 + 282;
                     y_min = 280 + 282; 
                }
                
                if(numeroParcours == 3)
                {
                     x_max = 600 + 640;
                     x_min = 40 + 640;
                     y_max = 400;
                     y_min = 280;
  
                }
                
                if(numeroParcours == 4)
                {
                     x_max = 600 + 640;
                     x_min = 40 + 640;
                     y_max = 400 + 282;
                     y_min = 280 + 282; 
                }
                
                
                
                DrawViewApercuChemin cheminView1 = new DrawViewApercuChemin(this, null,x_min,x_max,y_min,y_max);
                
                for(i=0;i<parcours.getSize();i++)
                {
                    cheminView1.parcours.getTab()[i] = new Point();
                    cheminView1.parcours.getTab()[i].setDistance(parcours.getTab()[i].getDistance());
                    cheminView1.parcours.getTab()[i].setHauteur(parcours.getTab()[i].getHauteur());
                    
                }
                cheminView1.parcours.setSize(i);
                cheminView1.miseEnFormeCheminTDF();
                cheminView1.invalidate();
                
                
                
                layout.getScrollX();
                cheminView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                layout.addView(cheminView1);
                
                
                
                return null;
            }
            
             public void initialisationText()
            {
                Typeface policeAltermove = Typeface.createFromAsset(getAssets(), "eurosti.ttf");
                
                ((TextView) findViewById(R.id.sousMenuChoix1Text1)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix1Text1)).setTextSize(30);
                ((TextView) findViewById(R.id.sousMenuChoix1Text2)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix1Text2)).setTextSize(25);
                ((TextView) findViewById(R.id.sousMenuChoix1Text3)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix1Text3)).setTextSize(20);
                
                ((TextView) findViewById(R.id.sousMenuChoix2Text1)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix2Text1)).setTextSize(30);
                ((TextView) findViewById(R.id.sousMenuChoix2Text2)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix2Text2)).setTextSize(25);
                ((TextView) findViewById(R.id.sousMenuChoix2Text3)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix2Text3)).setTextSize(20);
                
                ((TextView) findViewById(R.id.sousMenuChoix3Text1)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix3Text1)).setTextSize(30);
                ((TextView) findViewById(R.id.sousMenuChoix3Text2)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix3Text2)).setTextSize(25);
                ((TextView) findViewById(R.id.sousMenuChoix3Text3)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix3Text3)).setTextSize(20);
                
                ((TextView) findViewById(R.id.sousMenuChoix4Text1)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix4Text1)).setTextSize(30);
                ((TextView) findViewById(R.id.sousMenuChoix4Text2)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix4Text2)).setTextSize(25);
                ((TextView) findViewById(R.id.sousMenuChoix4Text3)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.sousMenuChoix4Text3)).setTextSize(20);
                
                ((TextView)findViewById(R.id.sousMenuErreurText)).setTypeface(policeAltermove);
                ((TextView)findViewById(R.id.sousMenuErreurText)).setTextSize(30);
                
            }
            
             public void evolutionCredit()
            {
                    if(credit == 0)
                    { 
                        exitActivity(view); 
                    }
                    
                    jaugeCreditD.weight = (credit);
                    findViewById(R.id.sousMenuCreditJaugeDroite).setLayoutParams(jaugeCreditD);

                    jaugeCreditG.weight = (creditInitial - credit);
                    findViewById(R.id.sousMenuCreditJaugeGauche).setLayoutParams(jaugeCreditG);

                    ((TextView) findViewById(R.id.sousMenuDistanceRestanteTextView)).setText(Integer.toString(credit)+" m");
            }
}